﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wx.Model.Message
{
    public static class Expand
    {
        public static string GetMessageString(this Text param)
        {
            string xml = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[{3}]]></MsgType>
<Content><![CDATA[{4}]]></Content>
</xml>";
            xml = string.Format(xml, param.ToUserName, param.FromUserName, param.CreateTime, param.MsgType, param.Content);
            return xml;
        }
        public static string GetMessageString(this Articles param)
        {
            string xml = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[1]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[{3}]]></MsgType>
<ArticleCount>{4}</ArticleCount>
{5}
</xml>";
            string items = @"<Articles>";
            foreach (ArticlesItem p in param.Items)
            {
                items += string.Format(@"<item>
<Title><![CDATA[{0}]]></Title>
<Description><![CDATA[{1}]]></Description>
<PicUrl><![CDATA[{2}]]></PicUrl>
<Url><![CDATA[{3}]]></Url>
</item>", p.Title, p.Description, p.PicUrl, p.Url);
            }
            items += "</Articles>";
            xml = string.Format(xml, param.ToUserName, param.FromUserName, param.CreateTime, param.MsgType, param.ArticleCount, items);
            return xml;
        }
        public static string GetMessageString(this Image param)
        {
            string xml = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[{3}]]></MsgType>
<Image>
<MediaId><![CDATA[{4}]]></MediaId>
</Image>
</xml>";
            xml = string.Format(xml, param.ToUserName, param.FromUserName, param.CreateTime, param.MsgType, param.MediaId);
            return xml;
        }
        public static string GetMessageString(this Music param)
        {
            string xml = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[{3}]]></MsgType>
<Music>
<Title><![CDATA[{4}]]></Title>
<Description><![CDATA[{5}]]></Description>
<MusicUrl><![CDATA[{6}]]></MusicUrl>
<HQMusicUrl><![CDATA[{7}]]></HQMusicUrl>
<ThumbMediaId><![CDATA[{8}]]></ThumbMediaId>
</Music>
</xml>";
            xml = string.Format(xml, param.ToUserName, param.FromUserName, param.CreateTime, param.MsgType, param.Title, param.Description, param.MusicUrl, param.HQMusicUrl, param.ThumbMediald);
            return xml;
        }

        public static string GetMessageString(this Video param)
        {
            string xml = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[{3}]]></MsgType>
<Video>
<MediaId><![CDATA[{4}]]></MediaId>
<Title><![CDATA[{5}]]></Title>
<Description><![CDATA[{6}]]></Description>
</Video> 
</xml>";
            xml = string.Format(xml, param.ToUserName, param.FromUserName, param.CreateTime, param.MsgType, param.MediaId, param.Title, param.Description);
            return xml;
        }

        public static string GetMessageString(this Voice param)
        {
            string xml = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[{3}]]></MsgType>
<Voice>
<MediaId><![CDATA[{4}]]></MediaId>
</Voice>
</xml>";
            xml = string.Format(xml, param.ToUserName, param.FromUserName, param.CreateTime, param.MsgType, param.MediaId);
            return xml;
        }

        public static string GetMessageString(this Template param)
        {
            try
            {
                return Newtonsoft.Json.JsonConvert.SerializeObject(param);
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}
